﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CuaHangMayTinh.BusinessEntities;
using CuaHangMayTinh.Utilities;
using CuaHangMayTinh.DataProvider;
using System.Data;

namespace CuaHangMayTinh.DataAccessLayer
{
    /// <summary>
    /// 
    /// </summary>
    public class ChiTietXuatKhoTraHangDA
    {
        /// <summary>
        /// 
        /// </summary>
        public ChiTietXuatKhoTraHangDA()
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Insert(ChiTietXuatKhoTraHang entity)
        {
            try
            {
                ParameterBuilder pb = DBFactory.CreateParamBuilder();

                pb.AddParameter("Ma_XK_TH", entity.Ma_XK_TH);
                pb.AddParameter("MaModelMH", entity.MaModelMH);
                pb.AddParameter("SoLuong", entity.SoLuong);
                pb.AddParameter("Ma_XK_TH", entity.Gia);
                pb.AddParameter("GhiChu", entity.GhiChu);

                return (int)DBFactory.Database.ExecuteNonQuery("ChiTietXuatKhoTraHang_Insert", pb.Parameters);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return 0;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="conn"></param>
        /// <param name="tran"></param>
        /// <returns></returns>
        public static int Insert(ChiTietXuatKhoTraHang entity,  DbConnector conn, System.Data.Common.DbTransaction tran)
        {
            try
            {
                ParameterBuilder pb = DBFactory.CreateParamBuilder();

                pb.AddParameter("Ma_XK_TH", entity.Ma_XK_TH);
                pb.AddParameter("MaModelMH", entity.MaModelMH);
                pb.AddParameter("SoLuong", entity.SoLuong);
                pb.AddParameter("Gia", entity.Gia);
                pb.AddParameter("GhiChu", entity.GhiChu);

                return (int)DBFactory.Database.ExecuteNonQuery("ChiTietXuatKhoTraHang_Insert", pb.Parameters, conn, tran);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return 0;
            }
        }

        public DataTable GetInfo_ByMaXuatKhoTraHang(int MaXuatKhoTraHang)
        {
            try
            {
                DataTable dt = new DataTable();
                ParameterBuilder pd = DBFactory.CreateParamBuilder();
                pd.AddParameter("MaXuatKhoTraHang", MaXuatKhoTraHang);

                return DBFactory.Database.FillDataTable("ChiTietBanHang_GetInfo_ByMaXuatKhoTraHang", pd.Parameters);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return null;
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public List<ChiTietXuatKhoTraHang> ConvertToList(DataTable dt)
        {
            List<ChiTietXuatKhoTraHang> list = new List<ChiTietXuatKhoTraHang>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                int id = (int)dt.Rows[i][0];
                int mahd = (int)dt.Rows[i][1];
                int mamodel = (int)dt.Rows[i][2];
                int soluong = (int)dt.Rows[i][3];
                decimal gia = (decimal)dt.Rows[i][4];
                string ghichu = dt.Rows[i][5].ToString();

                ChiTietXuatKhoTraHang entiry = new ChiTietXuatKhoTraHang(id, mahd, mamodel, soluong, gia, ghichu);
                list.Add(entiry);
            }
            return list;
        }

    }
}

